import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;

import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;


public class Main {
	
	public static void main(String[] args) throws IOException {
		
		String[] paths;
		
		if(args.length == 0){
			Scanner in = new Scanner(System.in);
			System.out.print("Arquivos? ");
			paths = in.nextLine().split(" ");;			
		}else{
			paths = args;
		}
		
		HashMap<String, Fix> fixs = new HashMap<String, Fix>();
		HashMap<String, Classe> classes = new HashMap<String, Classe>();
		System.out.println("Begin");
		
		for (String path : paths) {
		
			BufferedReader in = new BufferedReader(new FileReader(path));
			String line;
			while((line = in.readLine()) != null){
				
				if(line.contains("<compilationunit")){
					
					Classe classe;
					
					String className = line.trim().split(" ")[1].replace("base=", "").replace("\"", "").replace(".java", "");
					if(!classes.containsKey(className)){
						classe = new Classe();
						classes.put(className, classe);
					}else{
						classe = classes.get(className);
					}
					
					while(!(line = in.readLine()).contains("</compilationunit>")){
						
						if(line.contains("<fix")){
							
							Fix fix;
							
							String fixId = line.trim().split(" ")[2].replace("bug_id=", "").replace("\"", "");
							if(!fixs.containsKey(fixId)){
								fix = new Fix();
								fixs.put(fixId, fix);
							}else{
								fix = fixs.get(fixId);
							}
							
							fix.classes.add(className);
							classe.fixs.add(fixId);
						}
						
					}
				}
			}
		}
		DescriptiveStatistics statsFix = new DescriptiveStatistics();
		Iterator<Fix> iteratorFix = fixs.values().iterator();
		Fix fix;
		while(iteratorFix.hasNext()){
			fix = iteratorFix.next();
			statsFix.addValue(fix.classes.size());
		}
		
		DescriptiveStatistics statsClasse = new DescriptiveStatistics();
		Iterator<Classe> iteratorClasse = classes.values().iterator();
		Classe classe;
		while(iteratorClasse.hasNext()){
			classe = iteratorClasse.next();
			statsClasse.addValue(classe.fixs.size());
		}
		
		System.out.println("From Fix to Classe");
		System.out.println("mean, max, min, median");
		System.out.println(statsFix.getMean() + " " + statsFix.getMax() + " " + statsFix.getMin() + " " + statsFix.getPercentile(50));
		
		System.out.println("===========================================");
		
		System.out.println("From Classe to Fix");
		System.out.println("mean, max, min, median");
		System.out.println(statsClasse.getMean() + " " + statsClasse.getMax() + " " + statsClasse.getMin() + " " + statsClasse.getPercentile(50));
		
		
		//Save in xml
		/*
		XStream xml = new XStream();
		
		BufferedWriter out = new BufferedWriter(new FileWriter("out.xml"));
		xml.toXML(classes, out);
		out.newLine();
		xml.toXML(fixs, out);
		out.close();
		*/
		System.out.println("End.");
	}

}
